//
//  SelectedFenZuAlertView.m
//  SanYiTeaching
//
//  Created by 李文强 on 2019/5/14.
//  Copyright © 2019 SanYi. All rights reserved.
//

#import "SelectedFenZuAlertView.h"

@interface SelectedFenZuAlertView ()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (strong, nonatomic) UIPickerView *picker;
@property (strong, nonatomic) NSMutableArray<NSString *> *data;
@property (assign, nonatomic) NSInteger selectValue;  
@property (strong, nonatomic) UIButton *quxiaoBtn;


@end

@implementation SelectedFenZuAlertView

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.frame = CGRectMake(0, 0, 300, 300);
    [self setupCalendarView];
}
- (void)setupCalendarView {
    [self.view addSubview:self.picker];
    
    [self.view addSubview:self.quxiaoBtn];
    [self.quxiaoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.height.equalTo(@40);
        make.width.equalTo(self.view).multipliedBy(0.5);
        make.bottom.equalTo(@0).offset(-10);
        
    }];
    
    [self.view addSubview:self.confirmBtn];
    [_confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(@20);
        make.width.equalTo(self.view).multipliedBy(0.5);
        make.right.equalTo(@0).offset(0);
        make.height.equalTo(@40);
        make.bottom.equalTo(@0).offset(-10);
    }];
}
//选择器的初始化和布局
- (UIPickerView *)picker{
    if (!_picker) {
        _picker = [[UIPickerView alloc] initWithFrame:CGRectMake(30,0, 280, 300)];
        _picker.showsSelectionIndicator = YES;
        //设置代理
        _picker.delegate =self;
        _picker.dataSource =self;
    }
    return _picker;
}
//选择器数据的加载，从设定的最小日期到当前月
- (NSMutableArray<NSString *> *)data{
    if (!_data) {
        _data = [[NSMutableArray alloc] init];
        //循环获取可选月份，从当前月份到最小月份
        for (int i=0; i<_dataArray.count; i++) {
            SYWenTiFenZuModel *model = _dataArray[i];
            [_data addObject:model.sortName];
        }
        SYWenTiFenZuModel *model = _dataArray.firstObject;
        self.selectValue = [model.sortId integerValue];
    }
    return _data;
}
- (UIButton *)confirmBtn{
    if (!_confirmBtn) {
        UIButton *sender = [UIButton buttonWithType:UIButtonTypeCustom];
        [sender setTitle:@"确定" forState:UIControlStateNormal];
        [sender setTitleColor:SELECTEDCOLOR forState:UIControlStateNormal];
       // [sender setBackgroundImage:[UIImage imageWithColor:SELECTEDCOLOR forSize:CGSizeMake(120, 40)] forState:UIControlStateNormal];
        sender.layer.cornerRadius = 4;
        sender.titleLabel.font = [UIFont systemFontOfSize:14];
        sender.layer.masksToBounds = YES;
        [sender addTarget:self action:@selector(confirmClick:) forControlEvents:UIControlEventAllEvents];
        _confirmBtn = sender;
    }
    return _confirmBtn;
}
- (UIButton *)quxiaoBtn{
    if (!_quxiaoBtn) {
        UIButton *sender = [UIButton buttonWithType:UIButtonTypeCustom];
        [sender setTitle:@"取消" forState:UIControlStateNormal];
        [sender setTitleColor:SELECTEDCOLOR forState:UIControlStateNormal];
        //[sender setBackgroundImage:[UIImage imageWithColor:SELECTEDCOLOR forSize:CGSizeMake(120, 40)] forState:UIControlStateNormal];
        sender.layer.cornerRadius = 4;
        sender.titleLabel.font = [UIFont systemFontOfSize:14];
        sender.layer.masksToBounds = YES;
        [sender addTarget:self action:@selector(quxiaobtnClick) forControlEvents:UIControlEventAllEvents];
        _quxiaoBtn = sender;
    }
    return _quxiaoBtn;
}
#pragma mark - UIPickerView的数据和布局，和tableview类似
//返回多少列
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

//返回多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return self.data.count;
}

//每一行的数据
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return self.data[row];
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        [pickerLabel setTextAlignment:NSTextAlignmentCenter];
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        pickerLabel.font = [UIFont systemFontOfSize:16];
    }
    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
    
}
//选中时的效果
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    SYWenTiFenZuModel *model = _dataArray[row];
    self.selectValue = [model.sortId integerValue];
}

//返回高度
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 35.0f;
}

//返回宽度
-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
    return 300*fw;
}

#pragma mark - 背景视图的点击事件
- (void)didTapBackgroundView:(UITapGestureRecognizer *)sender {
    //    [self dismissWithAnimation:NO];
}

#pragma mark - 弹出视图方法
- (void)showWithAnimation:(BOOL)animation {
    //1. 获取当前应用的主窗口
    
}
#pragma mark - 确定按钮
- (void)confirmClick:(UIButton *)sender{
    self.block(self.selectValue);
    [self dismissViewControllerAnimated:YES completion:nil];
}
//取消
- (void)quxiaobtnClick {
    [self dismissViewControllerAnimated:YES completion:nil];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
